﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using AllenalaMente.State;
using Microsoft.Phone.Controls;

namespace AllenalaMente.Menu
{
    // Tipologie di Bonus
    public enum BonusId
    {
        ExtraTime,
        ExtraPoints,
        Extra3,
        Extra4,
        Extra5,
        None
    }

    class BonusDescription
    {
        public BonusId id;
        public string name;
        public string description;
        public int cost;
        public string imagePath = "/AllenalaMente;component/Assets/Images/UI/gameBoh.jpg";
    }

    public partial class BonusMenu : PhoneApplicationPage
    {
        private GameState gameState;

        private int slot1 = -1;
        private int slot2 = -1;
        private int slot3 = -1;

        private int coins;

        private BonusDescription[] bonus = new BonusDescription[5];

        public BonusMenu()
        {
            InitializeComponent();

            PageTitle.Text = Locale.Strings.Bonus;

            gameState = GameStateManager.GetState();
            coins = gameState.Coins;

            textBlockCoins.Text = coins + " N€URI";

            bonus[0] = new BonusDescription() { id = BonusId.ExtraTime, name = "Extra Time", cost = 10, description = "15 sec in più", imagePath = "/AllenalaMente;component/Assets/Images/Items/Item01.png" };
            bonus[1] = new BonusDescription() { id = BonusId.ExtraPoints, name = "Extra Points", cost = 15, description = "Moltiplica i punti x 2", imagePath = "/AllenalaMente;component/Assets/Images/Items/Item02.png" };
            bonus[2] = new BonusDescription() { id = BonusId.Extra3, name = "Extra ...", cost = 20, description = "Solo nella versione Delux", imagePath = "/AllenalaMente;component/Assets/Images/Items/Item03.png" };
            bonus[3] = new BonusDescription() { id = BonusId.Extra4, name = "Extra ...", cost = 30, description = "Solo nella versione Delux", imagePath = "/AllenalaMente;component/Assets/Images/Items/Item05.png" };
            bonus[4] = new BonusDescription() { id = BonusId.Extra5, name = "Extra ...", cost = 50, description = "Solo nella versione Delux", imagePath = "/AllenalaMente;component/Assets/Images/Items/Item07.png" };

            rectangleBonus1.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleBonus_MouseLeftButtonDown);
            rectangleBonus2.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleBonus_MouseLeftButtonDown);
            rectangleBonus3.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleBonus_MouseLeftButtonDown);
            rectangleBonus4.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleBonus_MouseLeftButtonDown);
            rectangleBonus5.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleBonus_MouseLeftButtonDown);

            rectangleSlot1.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleSlot_MouseLeftButtonDown);
            rectangleSlot2.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleSlot_MouseLeftButtonDown);
            rectangleSlot3.MouseLeftButtonDown += new MouseButtonEventHandler(rectangleSlot_MouseLeftButtonDown);

            Animation.FadeIn(this, 500);
        }

        void rectangleSlot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rect = (Rectangle)sender;

            // Carica Immagine
            Uri uri = new Uri("", UriKind.RelativeOrAbsolute);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);

            switch (rect.Name)
            {
                case "rectangleSlot1":
                    if (slot1 != -1)
                    {
                        imageSlot1.SetValue(Image.SourceProperty, img);

                        coins += bonus[slot1].cost;
                        textBlockCoins.Text = coins + " Coins";

                        slot1 = -1;

                        gameState.Bonus1 = BonusId.None;

                    }
                    break;

                case "rectangleSlot2":
                    if (slot2 != -1)
                    {
                        imageSlot2.SetValue(Image.SourceProperty, img);

                        coins += bonus[slot2].cost;
                        textBlockCoins.Text = coins + " Coins";

                        slot2 = -1;

                        gameState.Bonus1 = BonusId.None;
                    }
                    break;

                case "rectangleSlot3":
                    if (slot3 != -1)
                    {
                        imageSlot3.SetValue(Image.SourceProperty, img);

                        coins += bonus[slot3].cost;
                        textBlockCoins.Text = coins + " Coins";

                        slot3 = -1;

                        gameState.Bonus1 = BonusId.None;
                    }
                    break;
            }
        }

        void rectangleBonus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rect = (Rectangle)sender;
            int index = 0;

            rectangleBonus1.Stroke = new SolidColorBrush(Colors.White);
            rectangleBonus2.Stroke = new SolidColorBrush(Colors.White);
            rectangleBonus3.Stroke = new SolidColorBrush(Colors.White);
            rectangleBonus4.Stroke = new SolidColorBrush(Colors.White);
            rectangleBonus5.Stroke = new SolidColorBrush(Colors.White);

            rect.Stroke = new SolidColorBrush(Colors.Yellow);

            switch (rect.Name)
            { 
                case "rectangleBonus1":
                    index = 0;
                    break;

                case "rectangleBonus2":
                    index = 1;
                    break;

                case "rectangleBonus3":
                    index = 2;
                    break;

                case "rectangleBonus4":
                    index = 3;
                    break;

                case "rectangleBonus5":
                    index = 4;
                    break;
            }

            textBlockBonusDescription.Text = bonus[index].name + " " + bonus[index].cost + " Coins \n" + bonus[index].description;

            setSlot(index);
        }

        private void setSlot(int value)
        {
            // Carica Immagine
            Uri uri = new Uri(bonus[value].imagePath, UriKind.RelativeOrAbsolute);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);

            if (coins < bonus[value].cost) return;

            if (slot1 == value || slot2 == value || slot3 == value) return;

            if (slot1 == -1)
            {
                slot1 = value;

                imageSlot1.SetValue(Image.SourceProperty, img);

                coins -= bonus[value].cost;
                textBlockCoins.Text = coins + " Coins";

                gameState.Bonus1 = (BonusId)value;

                return;
            }

            if (slot2 == -1)
            {
                slot2 = value;

                imageSlot2.SetValue(Image.SourceProperty, img);

                coins -= bonus[value].cost;
                textBlockCoins.Text = coins + " Coins";

                gameState.Bonus2 = (BonusId)value;

                return;
            }

            if (slot3 == -1)
            {
                slot3 = value;

                imageSlot3.SetValue(Image.SourceProperty, img);

                coins -= bonus[value].cost;
                textBlockCoins.Text = coins + " Coins";

                gameState.Bonus3 = (BonusId)value;

                return;
            }
        }

        private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            gameState.Coins = coins;

            NavigationService.Navigate(new Uri("/AllenalaMente;component/Menu/GameSelection.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}